<?php

namespace Biz\service\advertisement\apiModule;

use Doraemon\model\Position\PositionCarousel;
use Doraemon\model\TopicActivity;
use Doraemon\tools\ImgUrl;

class HomeFollowCarousel extends BaseModule
{
    const FORMAT_HIT = 'formatPosHit';

    public function format($hit, $uid, $userProfileExt, $statSignExt)
    {
        $response = [];

        if ($hit) {
            $response = array_map(function ($hit) use ($uid) {
                $topic_id = $hit['ext']['topic_id'] ?? 0;
                $icon = $hit['ext']['icon'] ?? 0;
                return [
                    'id' => $hit['id'],
                    'index' => intval($hit['pos']),
                    'obj_id' => $topic_id,
                    'remark' => $hit['remark'],
                    'ext' => $icon,
                    'link_params' => $hit['link_params'],
                    'link_type' => $hit['link_type'],
                ];
            }, $hit);
        }

        $response = array_values($response);
        return $response;
    }

    public function getExtInfo($baseInfo)
    {
        return [
            'hot' => PositionCarousel::get('icon', ['position_base_info_id' => $baseInfo['id']]),
        ];
    }

    public function extListMap($ids)
    {
        $extList = PositionCarousel::select_master('*', ['position_base_info_id' => $ids]);
        return empty($extList) ? [] : array_column($extList, null, 'position_base_info_id');
    }

    public function formatAdmListExtInfo(&$v, $map)
    {
        $ext = $map[$v['id']] ?? [];;
        $title = TopicActivity::get('title', ['id' => $ext['topic_id']]);
        $v['hot'] = $ext['icon'] == PositionCarousel::ICON_NONE ? '无' : '展示';
        $v['topic_title'] = $title;
    }

    public function uniquePos($v): string
    {
        return $v['pos'];
    }

}